;
(function() {

	module.exports = {
		prefix: "/user/achs",
		get: {
			//获取当前用户的购买历史
			"/": function() {
				var user = role.getUser(this);
				var query = this.request.query;
				var achs = user.getAchs(query.num, query.page);
				this.body = ResponObj("json", achs);
			},
			"/detail": function() {
				var user = role.getUser(this);
				var query = this.request.query;
				var achs = user.getAchsDetail(query.num, query.page);
				this.body = ResponObj("json", achs);
			},
			"/with_page_info": function() {
				var user = role.getUser(this);
				var query = this.request.query;
				var info = user.getAchsDetail_PageInfo(query.num, query.page)
				this.body = ResponObj("json", info);
			},
			"/with_type_and_page_info": function() {
				var user = role.getUser(this);
				var query = this.request.query;
				var info = user.getAchsWithType_PageInfo(query.type, query.num, query.page)
				this.body = ResponObj("json", info);
			},
			"/id/:ach_id": function() {
				var user = role.getUser(this);
				var ach_id = this.params.ach_id;
				var ach = user.getAchById(ach_id);
				this.body = ResponObj("json", ach);
			},
			"/with_eval/id/:ach_id": function() {
				var user = role.getUser(this);
				var ach_id = this.params.ach_id;
				var ach = user.getAchWithEvalById(ach_id);
				this.body = ResponObj("json", ach);
			},
			// "/cancelAch/:ach_id":function () {
			// 	try{
			// 		var user  = role.getUser(this);
			// 	}catch(errorMsg){
			// 		this.body = ResponObj("error",errorMsg);
			// 		return;
			// 	}
			// 	var ach_id = this.params.ach_id;
			// 	if(user.removeAch(ach_id)){
			// 		this.body = ResponObj("json","已成功取消订单");
			// 	}else{
			// 		this.body = ResponObj("error","找不到指定商品");
			// 	}
			// }
			"/callback_pay": function() {}
		},
		put: {
			"/status/ol未收货_to_ol交易完成/:ach_id": function() {
				var user = role.getUser(this);
				var ach_id = this.params.ach_id;
				if (ach_id) {
					var result_status = user.state_ach_ol未收货_to_ol交易完成(ach_id);
					this.body = ResponObj("json", result_status);
				} else {
					throwCusError("参数错误")
				}
			},
			"/status/已发货_to_未收货未付款/:ach_id": function() {
				var user = role.getUser(this);
				var ach_id = this.params.ach_id;
				if (ach_id) {
					var result_status = user.state_ach_已发货_to_未收货未付款(ach_id);
					this.body = ResponObj("json", result_status);
				} else {
					throwCusError("参数错误")
				}
			},
			"/status/ol交易完成_to_ol已评价/:ach_id": function() {
				var user = role.getUser(this);
				var ach_id = this.params.ach_id;
				var evaluations = this.request.body.evaluations;
				if (!(evaluations instanceof Array)) {
					throwCusError("参数错误")
				}
				if (ach_id) {
					var result_status = user.state_ach_ol交易完成_to_ol已评价(ach_id, evaluations);
					this.body = ResponObj("json", result_status);
				} else {
					throwCusError("参数错误")
				}
			},
			"/status/交易完成_to_已评价/:ach_id": function() {
				var user = role.getUser(this);
				var ach_id = this.params.ach_id;
				var evaluations = this.request.body.evaluations;
				if (!(evaluations instanceof Array)) {
					throwCusError("参数错误")
				}
				if (ach_id) {
					var result_status = user.state_ach_交易完成_to_已评价(ach_id, evaluations);
					this.body = ResponObj("json", result_status);
				} else {
					throwCusError("参数错误")
				}
			},
			"/status/face交易完成_to_face已评价/:ach_id": function() {
				var user = role.getUser(this);
				var ach_id = this.params.ach_id;
				var evaluations = this.request.body.evaluations;
				if (!(evaluations instanceof Array)) {
					throwCusError("参数错误")
				}
				if (ach_id) {
					var result_status = user.state_ach_face交易完成_to_face已评价(ach_id, evaluations);
					this.body = ResponObj("json", result_status);
				} else {
					throwCusError("参数错误")
				}
			},
			//使用优惠码
			"/bind_coupon": function() {
				var user = role.getUser(this);
				var body = this.request.body;
				var ach_id = body.ach_id;
				var coupon_id = body.coupon_id;
				if (!(ach_id && coupon_id)) {
					throwCusError("参数错误")
				}
				user.bindCouponToAch(coupon_id, ach_id);
				var ach = user.getAchById(ach_id);
				this.body = ResponObj("json", ach);
			},
			//使用优惠券
			"/bind_ticket": function() {
				var user = role.getUser(this);
				var body = this.request.body;
				var ach_id = body.ach_id;
				var ticket_id = body.ticket_id;
				if (!(ach_id && ticket_id)) {
					throwCusError("参数错误")
				}
				user.bindTicketToAch(ticket_id, ach_id);
				var ach = user.getAchById(ach_id);
				this.body = ResponObj("json", ach);
			},
			//确认订单
			"/lock_ach/:ach_id": function() {
				var user = role.getUser(this);
				var ach_id = this.params.ach_id;
				var ach = user.lockAch(ach_id);
				this.body = ResponObj("json", ach);
			},
			//是否空间
			"/change_use_space/:ach_id": function() {
				var user = role.getUser(this);
				var ach_id = this.params.ach_id;
				var use_space = this.request.body.use_space;
				var ach = user.updateUseSpace(ach_id, use_space);
				this.body = ResponObj("json", ach);
			},
			//修改使用空间
			"/change_space/:ach_id": function() {
				var user = role.getUser(this);
				var ach_id = this.params.ach_id;
				var new_space_id = this.request.body.space_id;
				if (!new_space_id) {
					throwCusError("参数错误");
				}
				var ach = user.updateSpace(ach_id, new_space_id);
				this.body = ResponObj("json", ach);
			},
			//修改支付方式
			"/change_pay_method/:ach_id": function() {
				var user = role.getUser(this);
				var ach_id = this.params.ach_id;
				var new_pay_method = this.request.body.pay_method;
				var ach = user.updateAchPayMethod(ach_id, new_pay_method);
				this.body = ResponObj("json", ach);
			},
			//修改收获地址
			"/change_user_address/:ach_id": function() {
				var user = role.getUser(this);
				var ach_id = this.params.ach_id;
				var user_address_id = this.request.body.user_address_id;
				var user_address = user.updateUserAddress(ach_id, user_address_id);
				this.body = ResponObj("json", user_address);
			},
			//使用推荐返利的抵扣金额
			"/bind_acc": function() {
				var user = role.getUser(this);
				var body = this.request.body;
				var ach_id = body.ach_id;
				var bind_acc = body.bind_acc;
				if (!(ach_id && bind_acc)) {
					throwCusError("参数错误")
				}
				user.bindAccToAch(bind_acc, ach_id);
				var ach = user.getAchById(ach_id);
				this.body = ResponObj("json", ach);
			},
			//使用账户余额来抵扣订单
			"/use_bususer_money": function() {
				var user = role.getUser(this);
				var body = this.request.body;
				var ach_id = body.ach_id;
				var money = body.money;
				if (!(ach_id && money)) {
					throwCusError("参数错误")
				}
				user.bindBusUserMoneyToAch(ach_id, money);
				var ach = user.getAchById(ach_id);
				this.body = ResponObj("json", ach);
			}
		},
		"delete": {
			"/cancel_ach/:ach_id": function() {
				var user = role.getUser(this);
				var ach_id = this.params.ach_id;
				if (!ach_id) {
					throwCusError("参数错误")
				}
				user.cancelAch(ach_id);
				this.body = ResponObj("json", user.achs);
			}
		}
	};
	var role = require("./role");
	var BusCon = require("../../core/bus/_con");
	var UserCon = require("../../core/user/_con");
	var GoodsCon = require("../../core/goods/_con");
	var AchCon = require("../../core/ach/_con");
	var db = require("../../core/db/index");
	var config = require("../../core/_include/config");
	var ResponObj = require("../_include/responObj");

}());